package com.group.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.group.pojo.TbDevice;
import com.group.service.DeviceService;

@Controller
@RequestMapping("/device")
public class DeviceController {
	@Autowired
	private DeviceService deviceService;

	// 跳转设备管理页面
	@RequestMapping("/page.action")
	public String page() {
		return "info/device/device";
	}

	// 获得所有的设备信息
	@RequestMapping("/query.action")
	@ResponseBody
	public List<TbDevice> query(TbDevice device) {
		return deviceService.query(device);
	}
	
	//保存
	@RequestMapping("/save.action")
	@ResponseBody
	public Map<String, String> save(HttpServletRequest request, TbDevice device, MultipartFile deviceImg) {
		Map<String, String> map = deviceService.save(request, device, deviceImg);
		return map;
	}
	
	//保存
	@RequestMapping("/update.action")
	@ResponseBody
	public Map<String, String> update(TbDevice device) {
		Map<String, String> map = deviceService.update(device);
		return map;
	}
	
	
	@RequestMapping("/updateDeviceImg.action")
	@ResponseBody
	public Map<String, String> updateDeviceImg(TbDevice device, MultipartFile deviceImg) {
		Map<String, String> map = deviceService.updateDeviceImg(device, deviceImg);
		return map;
	}

	// 通过设备id查询设备信息
	@RequestMapping("/getDeviceById.action")
	@ResponseBody
	public List<TbDevice> getDeviceById(String Id) {
		return deviceService.getDeviceById(Id);
	}

	// 设备名称片段模糊查询
	@RequestMapping("/getDeviceInfoByNameFragment")
	@ResponseBody
	public List<TbDevice> getDeviceInfoByNameFragment(String nameFragment) {
		return null;// deviceService.getDeviceInfoByNameFragment(nameFragment);
	}

	// 通过设备名称查询设备id
	@RequestMapping("/getDeviceIdByDeviceServiceNumber.action")
	@ResponseBody
	public String getDeviceIdByDeviceServiceNumber(String deviceServiceNumber) {
		return deviceService.getDeviceIdByDeviceServiceNumber(deviceServiceNumber);
	}

	// 根据设备名查询设备是否存在
	@RequestMapping("/checkDeviceName.action")
	@ResponseBody
	public String checkDeviceName(@RequestParam String deviceName) {
		 try {
			deviceName = new String(deviceName.getBytes("iso-8859-1"), "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String str = deviceService.checkDeviceName(deviceName);
		return str;
	}
	// 添加新的设备
	@RequestMapping("/saveNewDevice.action")
	@ResponseBody
	public String saveNewDevice(TbDevice device, MultipartFile pictureFile, HttpServletRequest request) {
		String str = deviceService.saveNewDevice(device, pictureFile, request);
		return str;
	}

	// 修改设备信息
	@RequestMapping("/updateDevice.action")
	@ResponseBody
	public Map<String, String> updateDevice(TbDevice device, MultipartFile pictureFile1, HttpServletRequest request) {
		return deviceService.updateDevice(device, pictureFile1, request);
	}

	// 根据设备id删除设备
	@RequestMapping("/deleteDeviceInfo.action")
	@ResponseBody
	public Map<String, String> deleteDeviceInfo(@RequestParam(value = "deviceIds[]") String[] deviceIds) {
		return deviceService.deleteDeviceInfo(deviceIds);
	}

	// 添加设备时检查设备以及对应的服务号是否存在
	@RequestMapping("/CheckdeviceServiceNumber.action")
	@ResponseBody
	public String CheckdeviceServiceNumber(String deviceServiceNumber, String deviceName) {
		String str = deviceService.CheckdeviceServiceNumber(deviceServiceNumber, deviceName);
		return str;
	}

	// 根据设备id查询设备名称
	@RequestMapping("/getdeviceNameById.action")
	@ResponseBody
	public String getdeviceNameById(String deviceId) {
		String deviceName = deviceService.getdeviceNameById(deviceId);
		return deviceName;
	}

	// 修改设备时检查设备以及对应的服务号是否存在
	@RequestMapping("/updateCheckdeviceServiceNumber.action")
	@ResponseBody
	public String updateCheckdeviceServiceNumber(String deviceServiceNumber, String deviceName,String id) {
		String str = deviceService.updateCheckdeviceServiceNumber(deviceServiceNumber, deviceName,id);
		return str;
	}
}
